package com.wgchao.authmanager.domain.model;

import com.wgchao.authmanager.domain.params.LoginInfo;
import com.wgchao.authmanager.domain.repository.AdministratorRepository;
import com.wgchao.authmanager.infrastructure.exception.AdminNotExistException;
import com.wgchao.authmanager.infrastructure.exception.PwdNotMatchException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @作者：wgchao
 * @时间：22:18 2020/4/28 0028
 * @用途：身份认证
 **/
@Service
public class LoginAdminDomainService {
    @Autowired
    private AdministratorRepository administratorRepository;

    public LoginAdminInfo authentication(LoginInfo loginInfo){
        LoginAdmin loginAdmin = administratorRepository.get(loginInfo.getUname());
        if(loginAdmin == null){
            throw new AdminNotExistException("登录账户不存在");
        }
        boolean succ = loginAdmin.authentication(loginInfo.getPassword());
        if(!succ){
            throw new PwdNotMatchException("账户密码不匹配");
        }
        return loginAdmin.getInfo();
    }
}
